package com.lancoo.answer.widget.combinationView;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;
import com.lancoo.answer.util.paperUtils.ChildRightOrWrongJudgeUtils;
import com.lancoo.answer.util.paperUtils.DecimalFormatUtils;

/**
 * Created by pengmi on 2021/12/14 10:58
 * Description:填空题型查看评阅组合view
 */
public class FillAnswerResultView extends RelativeLayout {

    private static final String TAG = "AnswerAnalysisView";

    private boolean isHideScore;


    public FillAnswerResultView(Context context) {
        super(context);
        init(context);
    }

    public FillAnswerResultView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FillAnswerResultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.ev_layout_fill_answer_result,this);
        TextView tv_title=findViewById(R.id.tv_score_des);
        TextView tv_title1=findViewById(R.id.tv_answer_des);
        TextView tv_title2=findViewById(R.id.tv_my_score);
        TextView tv_title3=findViewById(R.id.tv_answer);
        String str="【";
        float v = tv_title.getPaint().measureText(str);
        tv_title.setTranslationX(-v/2);
        tv_title1.setTranslationX(-v/2);
        tv_title2.setTranslationX(-v/2);
        tv_title3.setTranslationX(-v/2);
        adjustFontSize();

    }

    public void setFillResult(Child child,String genreID){
        AnswerAnalysisView answerAnalysisView = findViewById(R.id.answer_analysis_view);
        answerAnalysisView.setAnalysis(child.getChildAnalysis());
        TextView textView=findViewById(R.id.tv_answer);
        String replaceStr=textView.getResources().getString(R.string.ev_standard_answer_replace);
        String childAnswer = child.getChildAnswer();
        if(child.getItemList()==null){
            return;
        }
        if(TextUtils.isEmpty(child.getChildAnswer())){
            childAnswer=child.getItemList().get(0).getItemAnswer();
        }
        if(childAnswer==null){
            childAnswer="";
        }
        textView.setText(childAnswer.replace("$/",replaceStr).replace("$",""));
        TextView tv_myScore=findViewById(R.id.tv_my_score);
        TextView tv_scorDes=findViewById(R.id.tv_score_des);
        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if(taskControlBean==null){
            return;
        }
        if(isHideScore||taskControlBean.getExpired()){
            tv_myScore.setVisibility(GONE);
            tv_scorDes.setVisibility(GONE);
            TextView tv_title=findViewById(R.id.tv_answer_des);
            LayoutParams layoutParams = (LayoutParams) tv_title.getLayoutParams();
            layoutParams.topMargin=0;
            tv_title.setLayoutParams(layoutParams);
            return;
        }
        float evalScore=0f;
        float totalScore=0f;

        for(Item item:child.getItemList()){
            if(item.getEvalScore()>0){
                evalScore+=item.getEvalScore();
            }
            totalScore+=item.getTotalScore();
        }
        String s = DecimalFormatUtils.RoundToTheNearestDecimalPlace1(evalScore);
        String totalScores = DecimalFormatUtils.RoundToTheNearestDecimalPlace1(totalScore);


        int state= ChildRightOrWrongJudgeUtils.judgeChildAnswerState(child);
        Log.e(TAG, "setFillResult: "+state );
        switch (state){
            case 0:
                tv_myScore.setTextColor(0xffff4d4d);
                tv_scorDes.setTextColor(0xffff4d4d);
                tv_myScore.setText(String.format("%s分", s));
                break;
            case 1:
                tv_myScore.setTextColor(0xff00c063);
                tv_scorDes.setTextColor(0xff00c063);
                tv_myScore.setText(String.format("%s分", s));
                break;
            case 2:
                tv_myScore.setText("--");
                tv_myScore.setTextColor(0xff0099ff);
                tv_scorDes.setTextColor(0xff0099ff);
                break;
        }
        if(TextUtils.equals("14",genreID)){//翻译选择特殊处理
            tv_myScore.setText(String.format("%s分", s+"/"+totalScores));
            if(evalScore>0){
                tv_myScore.setTextColor(0xff00c063);
                tv_scorDes.setTextColor(0xff00c063);
            }
        }
    }

    public void setHideScore(boolean hideScore) {
        isHideScore = hideScore;
    }

    public void adjustFontSize(){
        TextView tv_title=findViewById(R.id.tv_score_des);
        TextView tv_title1=findViewById(R.id.tv_answer_des);
        TextView tv_title2=findViewById(R.id.tv_my_score);
        TextView tv_title3=findViewById(R.id.tv_answer);

        AnswerAnalysisView answerAnalysisView=findViewById(R.id.answer_analysis_view);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv_title,14);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv_title1,14);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv_title2,13);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv_title3,13);
        answerAnalysisView.adjustFontSize();
    }
}
